
package Objects;

import GameEngine.GameConstants;
import GameEngine.GameConstants.Effect;
import GameEngine.GameConstants.Graphic;
import GameEngine.GameConstants.Material;
import GameEngine.Vector;

/**
 *
 * @author mhfowler
 */
public class Water extends GameObject {


    /* constructor */
    public Water(Vector dir, Vector cen, Vector s) {
        super(dir, cen, s, Graphic.WATER);
        material = Material.WATER;
        acceleration = new Vector (0, GameConstants.GRAVITY_ACCELERATION);
        // isTangible = false;
    }
    
    /**
     * "Copy" constructor
     * @param ice
     */

    public Water(Ice ice) {
        super(ice.getDir(), ice.getCenter(), ice.getSize(), Graphic.WATER);
        material = Material.WATER;
        acceleration = new Vector(0, GameConstants.GRAVITY_ACCELERATION);
        isTangible = false;
        setCurrent(ice.getFalseVel());
    }

    /**
     * For water, move randomly perpendicularly to the body we collided with
     * @param offset
     * @param totalNorm
     */

    public void offset(Vector offset, float totalNorm) {
        if (Math.abs(offset.getNorm()) > 0) {
            super.offset(offset, totalNorm);
            velocity.add(offset.getOrthog().scaleNew(Math.signum(Math.random()-0.5) / offset.getNorm() / 4.0));
        }
    }

    /* the slowing effects of water will be determined by its velocity */
    public void setCurrent(Vector v) {
        v = velocity;
    }

     /** this is the method by which other objects will effect this object. There will be a switch case
     * for each effect. For this abstract object, there will be a default implementation for each effect.
     * Specifiec objects will overwrite this method, but within them their default switch case will
     * be super.receiveEffect(), which will cause these default effects. */
    @Override
    public void receiveEffect(Effect e, Vector d, int power) {
         switch (e) {
             case TRANSMUTE:
                 Ice ice = new Ice(this);
                 lev.createObject(ice);
                 this.receiveEffect(Effect.DESTROY, null, 0);
                 break;
             case SPEED:
                 changed = true;
                 velocity.scale(power);
                 break;
             case SLOW:
                 changed = true;
                 velocity.scale(1/power);
                 break;
             case REVERSE:
                 super.receiveEffect(e, d, power);
                 break;
             default:
                 // not effected 
         }
    }

    /** this method will be called on both objects in a collision wherever the collision checker detects
     * a collision. By default this method will be made up of a switch case of Material, but particular
     * objects may have more interesting and nefarious collision behavior, including calling receiveEffect
     * on the object they collided with, causing an effect on their group, or causing an effect on themself.
     * @param o
     */
    @Override
    public void collide(GameObject o) {
        switch (o.material) {
            case WALL:
                super.collide(o);
                break;
            case WIND:
                super.collide(o);
                break;
            default:
                o.receiveEffect(Effect.CURRENT, direction, (int) velocity.getNorm());
                break;
        }
    }



    @Override
    public void nextIteration() {
        if (lifetime >300) {
            receiveEffect(Effect.DESTROY, null, 0);
        }
        super.nextIteration();
    }


}
